<template>
  <div id="app">
    <!-- 顶部导航栏 -->
    <el-header class="app-header">
      <div class="header-content">
        <!-- 移动端菜单按钮 -->
        <el-button 
          v-if="isMobile" 
          @click="toggleSidebar" 
          class="mobile-menu-btn"
          :icon="Menu"
          size="large"
        />
        
        <h1 class="app-title">
          <el-icon><Reading /></el-icon>
          <span class="title-text">圣经 - 中文和合本</span>
        </h1>
        
        <div class="header-actions">
          <el-button @click="showSearchDialog = true" type="primary" :icon="Search">
            <span class="btn-text">搜索经文</span>
          </el-button>
        </div>
      </div>
    </el-header>

    <!-- 主要内容区域 -->
    <el-container class="main-container">
      <!-- 左侧书卷列表 -->
      <el-aside 
        :width="isMobile ? '100%' : '600px'" 
        class="book-sidebar"
        :class="{ 'mobile-sidebar': isMobile, 'sidebar-open': isMobile && sidebarOpen }"
      >
        <div class="sidebar-content">
          <!-- 移动端关闭按钮 -->
          <div v-if="isMobile" class="mobile-sidebar-header">
            <h3>选择书卷</h3>
            <div class="mobile-header-actions">
              <el-button @click="scrollToNewTestament" size="small" type="primary">
                新约
              </el-button>
              <el-button @click="closeSidebar" :icon="Close" circle />
            </div>
          </div>
          
          <div class="book-list-container">
            <!-- 旧约全书标题 -->
            <div class="testament-title">
              <h3>旧约全书 ({{ oldTestament.length }}卷)</h3>
            </div>
            
            <!-- 旧约书卷列表 -->
            <div class="book-list">
              <div
                v-for="book in oldTestament"
                :key="book.id"
                class="book-item"
                :class="{ active: selectedBook?.id === book.id }"
                @click="selectBook(book)"
              >
                <span class="book-name">{{ book.name }}</span>
                <span class="book-english">{{ book.english }}</span>
              </div>
            </div>
            
            <!-- 新约全书标题 -->
            <div class="testament-title" id="new-testament-title">
              <h3>新约全书 ({{ newTestament.length }}卷)</h3>
            </div>
            
            <!-- 新约书卷列表 -->
            <div class="book-list">
              <div
                v-for="book in newTestament"
                :key="book.id"
                class="book-item"
                :class="{ active: selectedBook?.id === book.id }"
                @click="selectBook(book)"
              >
                <span class="book-name">{{ book.name }}</span>
                <span class="book-english">{{ book.english }}</span>
              </div>
            </div>
          </div>
        </div>
      </el-aside>

      <!-- 右侧内容区域 -->
      <el-main class="content-main">
        <div v-if="!selectedBook" class="welcome-content">
          <el-empty description="请选择一本书卷开始阅读">
            <el-icon size="60" color="#909399"><Reading /></el-icon>
          </el-empty>
        </div>

        <div v-else class="bible-content">
          <!-- 书卷标题和章节选择 -->
          <div class="book-header">
            <div class="book-title-section">
              <h2 class="book-title">
                {{ selectedBook.name }}
                <small class="book-english-title">{{ selectedBook.english }}</small>
              </h2>
              
              <!-- 移动端书卷切换按钮 -->
              <el-button 
                v-if="isMobile" 
                @click="toggleSidebar" 
                :icon="Menu"
                size="small"
                class="mobile-book-switch"
              >
                切换书卷
              </el-button>
            </div>
            
            <!-- 章节选择器 -->
            <div class="chapter-selector">
              <el-select 
                v-model="selectedChapter" 
                placeholder="选择章节"
                @change="loadChapter"
                :style="{ width: isMobile ? '100%' : '120px' }"
              >
                <el-option
                  v-for="chapter in selectedBook.chapters"
                  :key="chapter.number"
                  :label="`第${chapter.number}章`"
                  :value="chapter.number"
                />
              </el-select>
              
              <!-- 章节导航按钮 -->
              <div class="chapter-nav">
                <el-button 
                  @click="prevChapter" 
                  :disabled="selectedChapter <= 1"
                  :icon="ArrowLeft"
                  size="small"
                >
                  <span class="nav-text">上一章</span>
                </el-button>
                <el-button 
                  @click="nextChapter" 
                  :disabled="selectedChapter >= selectedBook.chapters.length"
                  :icon="ArrowRight"
                  size="small"
                >
                  <span class="nav-text">下一章</span>
                </el-button>
              </div>
            </div>
          </div>

          <!-- 经文内容 -->
          <div v-if="currentChapter" class="chapter-content">
            <div class="chapter-title">
              <h3>第 {{ currentChapter.number }} 章</h3>
            </div>
            
            <div class="verses-container">
              <div
                v-for="verse in currentChapter.verses"
                :key="verse.number"
                class="verse-item"
                :id="`verse-${verse.number}`"
              >
                <span class="verse-number">{{ verse.number }}</span>
                <span class="verse-text">{{ verse.text }}</span>
              </div>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>

    <!-- 移动端遮罩层 -->
    <div 
      v-if="isMobile && sidebarOpen" 
      class="mobile-overlay"
      @click="closeSidebar"
    ></div>

    <!-- 搜索对话框 -->
    <el-dialog
      v-model="showSearchDialog"
      title="搜索经文"
      :width="isMobile ? '90%' : '600px'"
      :before-close="handleSearchClose"
      class="search-dialog"
    >
      <div class="search-content">
        <el-input
          v-model="searchKeyword"
          placeholder="请输入要搜索的关键词"
          :prefix-icon="Search"
          @keyup.enter="performSearch"
        />
        
        <div class="search-options">
          <el-checkbox v-model="searchInOldTestament">搜索旧约</el-checkbox>
          <el-checkbox v-model="searchInNewTestament">搜索新约</el-checkbox>
        </div>
        
        <el-button 
          @click="performSearch" 
          type="primary" 
          :loading="searching"
          style="margin-top: 16px; width: 100%"
        >
          搜索
        </el-button>
      </div>
      
      <!-- 搜索结果 -->
      <div v-if="searchResults.length > 0" class="search-results">
        <h4>搜索结果 ({{ searchResults.length }} 条)</h4>
        <div class="result-list">
          <div
            v-for="result in searchResults"
            :key="`${result.bookId}-${result.chapter}-${result.verse}`"
            class="result-item"
            @click="goToVerse(result)"
          >
            <div class="result-reference">
              {{ result.bookName }} {{ result.chapter }}:{{ result.verse }}
            </div>
            <div class="result-text">{{ result.text }}</div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, computed, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Reading, Search, Menu, Close, ArrowLeft, ArrowRight } from '@element-plus/icons-vue'
import bibleData from './data/bible.json'

export default {
  name: 'App',
  setup() {
    // 响应式数据
    const selectedBook = ref(null)
    const selectedChapter = ref(1)
    const showSearchDialog = ref(false)
    const searchKeyword = ref('')
    const searchInOldTestament = ref(true)
    const searchInNewTestament = ref(true)
    const searching = ref(false)
    const searchResults = ref([])
    
    // 移动端相关状态
    const isMobile = ref(false)
    const sidebarOpen = ref(false)

    // 计算属性
    const oldTestament = computed(() => bibleData.oldTestament)
    const newTestament = computed(() => bibleData.newTestament)
    
    const currentChapter = computed(() => {
      if (!selectedBook.value) return null
      return selectedBook.value.chapters.find(ch => ch.number === selectedChapter.value)
    })

    // 检测移动端
    const checkMobile = () => {
      isMobile.value = window.innerWidth <= 768
    }

    // 方法
    const selectBook = (book) => {
      selectedBook.value = book
      selectedChapter.value = 1
      loadChapter()
      
      // 移动端选择书卷后自动关闭侧边栏
      if (isMobile.value) {
        closeSidebar()
      }
    }

    const loadChapter = () => {
      // 滚动到顶部
      window.scrollTo(0, 0)
    }

    const prevChapter = () => {
      if (selectedChapter.value > 1) {
        selectedChapter.value--
        loadChapter()
      }
    }

    const nextChapter = () => {
      if (selectedChapter.value < selectedBook.value.chapters.length) {
        selectedChapter.value++
        loadChapter()
      }
    }

    // 移动端侧边栏控制
    const toggleSidebar = () => {
      sidebarOpen.value = !sidebarOpen.value
    }

    const closeSidebar = () => {
      sidebarOpen.value = false
    }

    const scrollToNewTestament = () => {
      // 滚动到新约部分
      const newTestamentTitle = document.getElementById('new-testament-title')
      if (newTestamentTitle) {
        newTestamentTitle.scrollIntoView({ behavior: 'smooth', block: 'start' })
      }
    }

    const performSearch = () => {
      if (!searchKeyword.value.trim()) {
        ElMessage.warning('请输入搜索关键词')
        return
      }

      searching.value = true
      searchResults.value = []

      // 模拟搜索延迟
      setTimeout(() => {
        const keyword = searchKeyword.value.toLowerCase()
        const results = []

        // 搜索旧约
        if (searchInOldTestament.value) {
          oldTestament.value.forEach(book => {
            book.chapters.forEach(chapter => {
              chapter.verses.forEach(verse => {
                if (verse.text.toLowerCase().includes(keyword)) {
                  results.push({
                    bookId: book.id,
                    bookName: book.name,
                    chapter: chapter.number,
                    verse: verse.number,
                    text: verse.text
                  })
                }
              })
            })
          })
        }

        // 搜索新约
        if (searchInNewTestament.value) {
          newTestament.value.forEach(book => {
            book.chapters.forEach(chapter => {
              chapter.verses.forEach(verse => {
                if (verse.text.toLowerCase().includes(keyword)) {
                  results.push({
                    bookId: book.id,
                    bookName: book.name,
                    chapter: chapter.number,
                    verse: verse.number,
                    text: verse.text
                  })
                }
              })
            })
          })
        }

        searchResults.value = results.slice(0, 50) // 限制结果数量
        searching.value = false

        if (results.length === 0) {
          ElMessage.info('未找到相关经文')
        }
      }, 500)
    }

    const goToVerse = (result) => {
      // 找到对应的书卷
      const allBooks = [...oldTestament.value, ...newTestament.value]
      const book = allBooks.find(b => b.id === result.bookId)
      
      if (book) {
        selectedBook.value = book
        selectedChapter.value = result.chapter
        showSearchDialog.value = false
        
        // 滚动到指定经文
        setTimeout(() => {
          const verseElement = document.getElementById(`verse-${result.verse}`)
          if (verseElement) {
            verseElement.scrollIntoView({ behavior: 'smooth', block: 'center' })
            verseElement.classList.add('highlight')
            setTimeout(() => {
              verseElement.classList.remove('highlight')
            }, 2000)
          }
        }, 100)
      }
    }

    const handleSearchClose = () => {
      showSearchDialog.value = false
      searchKeyword.value = ''
      searchResults.value = []
    }

    // 生命周期
    onMounted(() => {
      console.log('圣经应用已加载')
      console.log('旧约书卷数量:', oldTestament.value.length)
      console.log('新约书卷数量:', newTestament.value.length)
      checkMobile()
      window.addEventListener('resize', checkMobile)
    })

    onUnmounted(() => {
      window.removeEventListener('resize', checkMobile)
    })

    return {
      // 响应式数据
      selectedBook,
      selectedChapter,
      showSearchDialog,
      searchKeyword,
      searchInOldTestament,
      searchInNewTestament,
      searching,
      searchResults,
      isMobile,
      sidebarOpen,

      // 计算属性
      oldTestament,
      newTestament,
      currentChapter,

      // 图标
      Reading,
      Search,
      Menu,
      Close,
      ArrowLeft,
      ArrowRight,

      // 方法
      selectBook,
      loadChapter,
      prevChapter,
      nextChapter,
      toggleSidebar,
      closeSidebar,
      scrollToNewTestament,
      performSearch,
      goToVerse,
      handleSearchClose
    }
  }
}
</script>

<style scoped>
/* 应用整体样式 */
#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 顶部导航栏 */
.app-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1000;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.mobile-menu-btn {
  display: none;
  background: transparent;
  border: none;
  color: white;
  font-size: 20px;
}

.app-title {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.title-text {
  display: inline;
}

/* 主容器 */
.main-container {
  flex: 1;
  height: calc(100vh - 60px);
  overflow: visible;
}

/* 左侧边栏 */
.book-sidebar {
  background: #f8f9fa;
  border-right: 1px solid #e4e7ed;
  overflow-y: visible;
  min-height: 100%;
  max-height: none;
  transition: transform 0.3s ease;
}

.mobile-sidebar {
  position: fixed;
  top: 60px;
  left: 0;
  width: 100% !important;
  height: calc(100vh - 60px);
  z-index: 999;
  transform: translateX(-100%);
  background: white;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  overflow-y: auto; /* 添加滚动条支持 */
}

.sidebar-open {
  transform: translateX(0);
}

.mobile-sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #e4e7ed;
  background: #f8f9fa;
  position: sticky;
  top: 0;
  z-index: 10;
}

.mobile-sidebar-header h3 {
  margin: 0;
  color: #2c3e50;
}

.mobile-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-content {
  padding: 16px;
  height: auto;
  overflow-y: visible; /* 确保内容可以滚动 */
}

.book-list-container {
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding-bottom: 20px; /* 添加底部间距，确保新约部分可见 */
}

.testament-title {
  padding-left: 8px;
  margin-bottom: 8px;
}

.testament-title h3 {
  font-size: 16px;
  color: #2c3e50;
  margin: 0;
  padding-bottom: 8px;
  border-bottom: 2px solid #e0e0e0;
  font-weight: 600;
}

.book-list {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  overflow-y: visible;
  padding-bottom: 8px;
}

.book-item {
  padding: 6px 4px;
  margin: 0;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid transparent;
  display: flex;
  flex-direction: column;
  text-align: center;
  min-height: 50px;
  justify-content: center;
}

.book-item:hover {
  background: #e6f7ff;
  border-color: #91d5ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.book-item.active {
  background: #1890ff;
  color: white;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.3);
}

.book-name {
  display: block;
  font-weight: 500;
  font-size: 12px;
  line-height: 1.2;
  margin-bottom: 1px;
}

.book-english {
  display: block;
  font-size: 10px;
  opacity: 0.8;
  line-height: 1.0;
}

/* 主内容区域 */
.content-main {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}

.welcome-content {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

/* 圣经内容样式 */
.bible-content {
  max-width: 90%;
  margin: 0 auto;
}

.book-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 2px solid #f0f0f0;
}

.book-title-section {
  display: flex;
  align-items: center;
  gap: 16px;
}

.book-title {
  margin: 0;
  font-size: 28px;
  color: #2c3e50;
}

.book-english-title {
  font-size: 16px;
  color: #7f8c8d;
  font-weight: normal;
  margin-left: 8px;
}

.mobile-book-switch {
  display: none;
}

.chapter-selector {
  display: flex;
  align-items: center;
  gap: 16px;
}

.chapter-nav {
  display: flex;
  gap: 8px;
}

.nav-text {
  display: inline;
}

/* 章节内容 */
.chapter-content {
  background: white;
  border-radius: 12px;
  padding: 32px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.chapter-title {
  text-align: center;
  margin-bottom: 32px;
}

.chapter-title h3 {
  font-size: 24px;
  color: #2c3e50;
  margin: 0;
}

.verses-container {
  line-height: 2;
  font-size: 16px;
}

.verse-item {
  display: flex;
  margin-bottom: 16px;
  padding: 8px 0;
  transition: background-color 0.3s ease;
}

.verse-item.highlight {
  background: #fff7e6;
  border-left: 4px solid #faad14;
  padding-left: 12px;
}

.verse-number {
  font-weight: bold;
  color: #1890ff;
  margin-right: 12px;
  min-width: 30px;
  text-align: right;
}

.verse-text {
  flex: 1;
  color: #2c3e50;
}

/* 移动端遮罩层 */
.mobile-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 998;
}

/* 移动端滚动优化 */
.mobile-sidebar {
  -webkit-overflow-scrolling: touch; /* iOS滚动优化 */
  scroll-behavior: smooth;
}

.mobile-sidebar::-webkit-scrollbar {
  width: 4px;
}

.mobile-sidebar::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.mobile-sidebar::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 2px;
}

.mobile-sidebar::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 搜索对话框样式 */
.search-content {
  margin-bottom: 20px;
}

.search-options {
  margin-top: 16px;
  display: flex;
  gap: 16px;
}

.search-results {
  margin-top: 20px;
  border-top: 1px solid #e4e7ed;
  padding-top: 20px;
}

.search-results h4 {
  margin: 0 0 16px 0;
  color: #2c3e50;
}

.result-list {
  max-height: 300px;
  overflow-y: auto;
}

.result-item {
  padding: 12px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.result-item:hover {
  background: #f5f7fa;
  border-color: #1890ff;
}

.result-reference {
  font-weight: bold;
  color: #1890ff;
  margin-bottom: 4px;
}

.result-text {
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

/* 响应式设计优化 */
@media (max-width: 1200px) {
  .book-list {
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
  }
  
  .book-item {
    padding: 8px 6px;
    min-height: 55px;
  }
  
  .book-name {
    font-size: 13px;
  }
  
  .book-english {
    font-size: 11px;
  }
}

@media (max-width: 768px) {
  /* 显示移动端菜单按钮 */
  .mobile-menu-btn {
    display: block;
  }
  
  /* 隐藏桌面端标题文字 */
  .title-text {
    display: none;
  }
  
  /* 隐藏按钮文字 */
  .btn-text {
    display: none;
  }
  
  .nav-text {
    display: none;
  }
  
  /* 显示移动端书卷切换按钮 */
  .mobile-book-switch {
    display: block;
  }
  
  .book-sidebar {
    width: 100% !important;
    height: auto;
    max-height: none;
    overflow-y: visible;
  }
  
  .main-container {
    flex-direction: column;
    height: auto;
    overflow: visible;
  }
  
  .book-list-container {
    gap: 16px;
    /* 确保新约部分可见 */
    min-height: calc(100vh - 120px); /* 减去头部和padding的高度 */
    padding-bottom: 40px; /* 增加底部间距 */
  }
  
  .testament-title h3 {
    font-size: 15px;
  }
  
  /* 移动端改为3列布局 */
  .book-list {
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
  }
  
  .book-item {
    padding: 6px 4px;
    min-height: 50px;
  }
  
  .book-name {
    font-size: 12px;
  }
  
  .book-english {
    font-size: 10px;
  }
  
  .book-header {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }
  
  .chapter-selector {
    width: 100%;
    justify-content: space-between;
  }
  
  .chapter-content {
    padding: 16px;
  }
  
  .content-main {
    padding: 16px;
  }
  
  .bible-content {
    max-width: 100%;
  }
  
  .search-options {
    flex-direction: column;
    gap: 8px;
  }
}

@media (max-width: 480px) {
  .book-sidebar {
    max-height: 100%;
  }
  
  /* 超小屏幕改为2列布局 */
  .book-list {
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
  }
  
  .book-item {
    padding: 8px 6px;
    min-height: auto;
  }
  
  .book-name {
    font-size: 13px;
  }
  
  .book-english {
    font-size: 11px;
  }
  
  .testament-title h3 {
    font-size: 14px;
  }
  
  .book-title {
    font-size: 24px;
  }
  
  .chapter-content {
    padding: 12px;
  }
  
  .verses-container {
    font-size: 15px;
  }
}

@media (max-width: 320px) {
  /* 极小屏幕改为1列布局 */
  .book-list {
    grid-template-columns: 1fr;
    gap: 4px;
  }
  
  .book-item {
    padding: 8px 12px;
    text-align: left;
  }
  
  .book-title {
    font-size: 20px;
  }
  
  .chapter-content {
    padding: 8px;
  }
}

/* 搜索对话框移动端优化 */
@media (max-width: 768px) {
  .search-dialog {
    margin: 0;
  }
  
  .search-dialog .el-dialog {
    margin: 5vh auto;
  }
}
</style> 